package mysql

import (
	"fresh-box/freshrpc/freshrpc"
	"gorm.io/gorm"
)

type Fresh struct {
	gorm.Model
	Title  string  `gorm:"type:varchar(30);not null;comment:标题"`
	Price  float64 `gorm:"type:decimal(10,2);not null;comment:价格"`
	SortId int     `gorm:"type:int;not null;comment:分类id"`
}

func (f *Fresh) FreshAdd(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}

//
//func (f *Fresh) SortById(db *gorm.DB, id int64) error {
//	return db.Debug().Where("sort.id = ?", id).First(f).Error
//}

//func (f *Fresh) TitleByFresh(db *gorm.DB, title string) error {
//	return db.Debug().Where("title = ?", title).First(f).Error
//}

func FreshList(db *gorm.DB) ([]*freshrpc.FreshList, error) {
	var list []*freshrpc.FreshList
	err2 := db.Debug().Model(&Fresh{}).
		Select(
			"freshes.id as fresh_id",
			"title",
			"price",
			"sort_id",
		).Find(&list).Error
	return list, err2
}
